package com.zhhs.galaxyott.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.MoreMoviesActivity;
import com.zhhs.galaxyott.beans.CategoryBean;
import com.zhhs.galaxyott.beans.CategoryMoviesBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.CategoryMoviesResponse;
import com.zhhs.galaxyott.utils.Logs;

public class MoviesPagersAdapterTest extends PagerAdapter {

    private Context mContext;
    private static List<View> list = new ArrayList<View>();
    private List<CategoryBean> mlistData = new ArrayList<CategoryBean>();
    private static List<CategoryMoviesBean> PagerData = new ArrayList<CategoryMoviesBean>();
    private View view;
    private RelativeLayout itemLayout;

    private static LinearLayout mGridView;

    public MoviesPagersAdapterTest(Context mContext, List<View> list, List<CategoryBean> mlistData) {
        this.mContext = mContext;
        this.list = list;
        this.mlistData = mlistData;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Logs.logD("DEBUG_DT", "destroyItem-->" + position);
        container.removeView(list.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        container.addView(list.get(position));// 实例化页卡

        Logs.logD("DEBUG_DT", "THIS IS instantiateItem-->instantiateItem" + MoreMoviesActivity.selectId + "POSITION"
                + position + "selection:" + MoreMoviesActivity.mListView.getSelectedItemPosition());

        Logs.logD("DEBUG_DT", "THIS IS instantiateItem-->" + MoreMoviesActivity.selectId);
        // loadData(position, mlistData.get(position).getId());

        return list.get(position);
    }

    private void loadData(final int pos, int index) {

        final int categoryid = index;
        int pageNo = 1;
        int pageRow = 100;

        RequestMaker.getCategoryMovie((Activity) mContext, categoryid, pageNo, pageRow,
                new ResponseListener<CategoryMoviesResponse>() {

                    @Override
                    public void onResponse(CategoryMoviesResponse result) {
                        // if (result.Beans == null || result.Beans.size() == 0)
                        // {
                        // return;
                        // }

                        Logs.logD("DEBUG_DT",
                                "THIS IS getCategoryMovie-->" + result.errCode + "size" + result.Beans.size()
                                        + "categoryid" + categoryid);
                        if (result.errCode == 0) {

                            PagerData = result.Beans;

                            initGridView(pos, PagerData);

                        }

                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                });

    }

    public void initViews(int position) {
        loadData(position, mlistData.get(position).getId());
    }

    private void initGridView(int postion, List<CategoryMoviesBean> PagerData) {
        view = list.get(postion);
        mGridView = (LinearLayout) view.findViewById(R.id.gridview_records);

        int k = mlistData.size();
        int i = 0;
        int j = 0;
        Log.i("DEBUG_DD", "THIS IS -->initView");
        for (i = 0; i < (k + 1) / 5; i++) {

            LinearLayout layout = new LinearLayout(mContext);
            layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            layout.setOrientation(LinearLayout.HORIZONTAL);
            // 代码中new一个界面此处需要移出
            layout.removeAllViews();

            // 数据为单数时候的处理
            if ((k - i * 5) == 1) {
                itemLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.fragment_more_movies_item,
                        null, false);
                initItemView(itemLayout, i * 5);
                layout.addView(itemLayout);
            } else {
                for (j = 0; j < 5; j++) {
                    itemLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(
                            R.layout.fragment_more_movies_item, null, false);
                    initItemView(itemLayout, i * 5 + j);
                    layout.addView(itemLayout);
                }
            }

            mGridView.addView(layout);
        }

    }

    private static ImageView posterImg;

    private void initItemView(View v, int i) {
        posterImg = (ImageView) v.findViewById(R.id.poster);
        ImageView poster_tag = (ImageView) v.findViewById(R.id.poster_tag);
        TextView movieName = (TextView) v.findViewById(R.id.poster_name);

    }

    @Override
    public boolean isViewFromObject(View v, Object object) {
        return v == object;// 官方推荐
    }

    public static int index = 0;

    public static synchronized boolean onKeyDown(int pos, int keyCode, KeyEvent event) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:

            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            break;

        case KeyEvent.KEYCODE_DPAD_DOWN:
            posterImg.requestFocus();
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            break;
        default:
            break;
        }
        return false;

    }

    private static boolean isLeftView() {
        for (int i = 0; i < (((PagerData.size() - 1) / 5) + 1); i++) {
            if (mGridView.getChildAt(5 * i).hasFocus()) {
                return true;
            }
        }
        return false;
    }

}
